package com.wechat.scheme.service;

import cn.hutool.core.collection.CollectionUtil;
import com.wechat.scheme.entity.BaiduSource;
import com.wechat.scheme.entity.model.TextMessage;
import com.wechat.scheme.repo.BaiduSourceRepo;
import com.wechat.scheme.utils.MessageUtil;
import com.wechat.scheme.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CoreService {

    @Autowired
    private BaiduSourceRepo baiduSourceRepo;


    private static final String temple_str = "请检查名字是否正确\n" +
            "---------------\n" +
            "例如：王国<第二季>，则只需回复王国第二季即可\n" +
            "---------------\n" +
            "如正确，则暂未添加，我们将尽快更新";

    public  String parseWxRequest(HttpServletRequest request)  {
        // xml格式的消息数据
        String respXml = null;
        // 默认返回的文本消息内容
        String respContent = "未知的消息类型！";
        try {
            // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号,一个openID
            String fromUserName = requestMap.get("FromUserName");
            // 开发者微信号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 接收用户发送的文本消息内容
            String content = requestMap.get("Content");
            //回复文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            textMessage.setCreateTime(System.currentTimeMillis());
            textMessage.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_TEXT);

            // 文本消息
            if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
                if(null ==baiduSourceRepo){
                    baiduSourceRepo = SpringUtil.getBean(BaiduSourceRepo.class);
                }
                content = StringUtils.isEmpty(content) ? "" : content.trim();
                log.info(content);
                List<BaiduSource> queryList =  baiduSourceRepo.findByKeyWordLike(content);
                if(CollectionUtil.isNotEmpty(queryList)){
                    respContent = "已为小主马不停蹄找到资源:"+"\n";
                    respContent += queryList.stream().map(p -> p.getKeyWord()+"->"+p.getUrlAddr()).collect(Collectors.joining("\n"));
                    log.info(respContent);
                }else{
                    respContent = temple_str;
                }
                textMessage.setContent(respContent);
                String xml = MessageUtil.buildTextMessage(textMessage);
                respXml = xml;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return respXml;
    }
}
